package com.bestcem.xm.ticket.grpc.client;

import com.bestcem.xm.common.core.constant.GrpcClientNames;
import com.bestcem.xm.openplatform.grpc.v1.services.BaseOpenplatformServiceGrpc;
import com.bestcem.xm.openplatform.grpc.v1.services.FeiShuEnterpriseInfo;
import com.bestcem.xm.openplatform.grpc.v1.services.FeiShuEnterpriseInfoSearchResponse;
import com.bestcem.xm.openplatform.grpc.v1.services.ListFeiShuEnterpriseInfoRequest;
import com.bestcem.xm.ticket.grpc.client.convert.TicketBaseOpenPlatformConvert;
import com.bestcem.xm.ticket.grpc.client.dto.openPlatform.FeiShuEnterpriseInfoDTO;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import zhongyan.xm.base.v1.Base;

import javax.annotation.Nullable;
import java.util.List;
import java.util.Objects;

/**
 * Openplatform 模块基础接口rpc客户端
 *
 * @author zq.ding <zq.ding@idiaoyan.com>
 * @version v1.0
 * @date 2021/8/30 20:08
 */
@Slf4j
@Service
public class TicketBaseOpenPlatformGrpcService {

    @GrpcClient(GrpcClientNames.DS_OPENPLATFORM)
    private BaseOpenplatformServiceGrpc.BaseOpenplatformServiceBlockingStub baseOpenplatformServiceBlockingStub;

    /**
     * 获取FeiShuEnterpriseInfoDTO列表
     *
     * @param feiShuEnterpriseInfo 飞书详细信息
     * @param search               若空则不可搜索
     * @param status               若空则传空数组
     * @param fields               此参数设置后只返回指定的字段
     * @return java.util.List<com.bestcem.xm.ticket.grpc.client.dto.openPlatform.FeiShuEnterpriseInfoDTO>
     * @author zq.ding <zq.ding@idiaoyan.com>
     * @date 2021/8/31 17:15
     */
    @Nullable
    public List<FeiShuEnterpriseInfoDTO> listFeiShuEnterpriseInfo(FeiShuEnterpriseInfo feiShuEnterpriseInfo,
                                                                  String search, List<Integer> status, List<String> fields) {
        ListFeiShuEnterpriseInfoRequest.Builder builder = ListFeiShuEnterpriseInfoRequest.newBuilder();
        if (Objects.nonNull(feiShuEnterpriseInfo)) {
            builder.setFeiShuEnterpriseInfo(feiShuEnterpriseInfo);
        }
        if (!ObjectUtils.isEmpty(search)) {
            builder.setSearch(search);
        }
        if (!CollectionUtils.isEmpty(status)) {
            builder.addAllStatus(status);
        }
        if (!CollectionUtils.isEmpty(fields)) {
            builder.addAllFields(fields);
        }
        try {
            FeiShuEnterpriseInfoSearchResponse response = baseOpenplatformServiceBlockingStub.listFeiShuEnterpriseInfo(builder.build());
            if (Objects.isNull(response) || Base.Code.ERROR.equals(response.getStatus().getCode())) {
                log.error("[Ticket] OpenPlatform ListFeiShuEnterpriseInfo Error; feiShuEnterpriseInfoId: {} ErrorMsg: {}", feiShuEnterpriseInfo.getId(), response);
                return null;
            }
            return TicketBaseOpenPlatformConvert.responseToDTO(response);
        } catch (Exception e) {
            log.error("[Ticket] OpenPlatform ListFeiShuEnterpriseInfo Error; feiShuEnterpriseInfoId: {}", feiShuEnterpriseInfo.getId(), e);
            return null;
        }

    }

}
